package com.token.service.impl;

import com.token.domain.LoginUser;
import com.token.domain.User;
import com.token.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

@Service("userDetailsService")
public class UserDetailServiceImpl implements UserDetailsService {

    @Autowired
    private UserMapper userMapper;


    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {


        //查询用户信息


        User user = userMapper.queryUserByUserName(username);

      //  System.out.println("user:"+user);


        if (Objects.isNull(user) ){
            throw new RuntimeException("用户名或者密码错误");
        }

        System.out.println(user);




        //TODO 查询对应的权限信息

        List<String>  list=new ArrayList<>(Arrays.asList("test","admin"));

        




        //把数据封装成UserDetail

        return new LoginUser(user,list);
    }
}
